package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.*;
import Entity.*;

public class ListRefundProveServlet extends HttpServlet {

	private ListRefundProveDao listrefunddao = new ListRefundProveDao();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		String path = "pages/List_Refund_Prove.jsp";
		boolean ac = false;
		if(method.equals("list")){
			/*
			 * 1.如果是通过主页请求
			 * 		页面获得的时间为null，检索数据库获得信息最早时间和最晚时间
			 *		页面获得的页数为null，通过上一条获得时间范围，计算显示的页数Button
			 */
			if(request.getParameter("begin_time") == null && request.getParameter("end_time") == null){
				String begin_time = listrefunddao.select_begin_time().split(" ")[0];
				String end_time = listrefunddao.select_end_time().split(" ")[0];
				if(begin_time.equals("")){
					request.setAttribute("pages", 1);
					request.setAttribute("pages_num", 1);
				}else{
					select_time(request,response,begin_time,end_time);
				}
			}else {
				String begin_time = request.getParameter("begin_time");
				String end_time = request.getParameter("end_time");
				select_time(request,response,begin_time,end_time);
			}
		}
		// 恢复、作废凭证
		//1.作废-修改凭证的状态
		//2.金额退回
		else if(method.equals("nullify")){
			int id = Integer.parseInt(request.getParameter("id"));
			int state = Integer.parseInt(request.getParameter("state"));
			int code = Integer.parseInt(request.getParameter("code"));
			double to_money = Double.parseDouble(request.getParameter("money"));
			//1.修改凭证状态
			listrefunddao.nullify(id,state);
			//2.金额退回
			if(state == 0){
//				-
				double money = listrefunddao.getUserBalance(code);
				double changed_money = money - to_money;
				listrefunddao.change_money(code, changed_money);
			}else{
//				+
				double money = listrefunddao.getUserBalance(code);
				double changed_money = money + to_money;
				listrefunddao.change_money(code, changed_money);
			}
			path = "List_Refund_Prove_Servlet?method=list";
			ac = true;
		}
		if(ac){
			response.sendRedirect(path);
		}else{
			request.getRequestDispatcher(path).forward(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	public void select_time(HttpServletRequest request,HttpServletResponse response,String begin_time,String end_time){
		String[] begin_times = begin_time.split("-");
		request.setAttribute("begin_year_select", begin_times[0]);
		request.setAttribute("begin_month_select", begin_times[1]);
		request.setAttribute("begin_day_select", begin_times[2]);
		
		String[] end_times = end_time.split("-");
		request.setAttribute("end_year_select", end_times[0]);
		request.setAttribute("end_month_select", end_times[1]);
		request.setAttribute("end_day_select", end_times[2]);
		begin_time += " 00:00:00";
		end_time += " 23:59:59";
		
		
		//每页行数
		int pages_rows = 25;
		//获得总行数DAO
		int rows = listrefunddao.select_rowsBy_time(begin_time,end_time);
		//得出页数
		int pages = rows / pages_rows;
		if(rows % pages_rows > 0 && pages!=0){
			pages+=1;
		}
		if(pages == 0){
			pages = 1;
		}
		//获得请求要翻转至的页码
		int page = 1;
		if(request.getParameter("page")!=null){
			page = Integer.parseInt(request.getParameter("page"));
		}
		int begin = (page-1) * pages_rows;
//		List<Refund_Prove_Entity>list = listrefunddao.select_PagesRows(begin, pages_rows);
		//获得一定时间段内的 指定页数的 所有数据
		List<RefundProveEntity>list = listrefunddao.selectBytime_PagesRows(begin, pages_rows, begin_time, end_time);
		request.setAttribute("list", list);
		request.setAttribute("pages", pages);
		request.setAttribute("pages_num", page);
	}
}
